package it.clariter.dao;

import it.clariter.connection.MyConnection;
import it.clariter.model.Verso;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//Verso: id, codice, descrizione, documenti(ArrayList<Documento>)
public class VersoDAO {

	public int insert(Verso verso)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("INSERT INTO "+DaoConstants.NOME_TABELLA_VERSO+" SET "+DaoConstants.COLONNA_CODICE_VERSO+"=?,"+DaoConstants.COLONNA_DESCRIZIONE_VERSO+"=?");
			ps.setString(1, verso.getCodice());
			ps.setString(2, verso.getDescrizione());
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(Verso verso)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("UPDATE "+DaoConstants.NOME_TABELLA_VERSO+" SET "+DaoConstants.COLONNA_CODICE_VERSO+"=?,"+DaoConstants.COLONNA_DESCRIZIONE_VERSO+"=? WHERE "+DaoConstants.COLONNA_ID_VERSO+"=?");
			ps.setString(1, verso.getCodice());
			ps.setString(2, verso.getDescrizione());
			ps.setInt(3, verso.getIdVerso());
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public int delete(int idva)
	{
		Connection c=null;
		PreparedStatement ps=null;
		int result=0;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("DELETE FROM "+DaoConstants.NOME_TABELLA_VERSO+" WHERE "+DaoConstants.COLONNA_ID_VERSO+"=?");
			ps.setInt(1, idva);
			result=ps.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	public List<Verso>getList()
	{
		Connection c=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List <Verso>list=new ArrayList<Verso>();
		Verso verso=null;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("SELECT * FROM "+DaoConstants.NOME_TABELLA_VERSO);
			rs=ps.executeQuery();
			while(rs.next())
			{
				verso=new Verso();
				verso=makeObject(verso,rs);
				list.add(verso);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return list;
	}
	public Verso get(int idva)
	{
		Connection c=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Verso verso=null;
		try
		{
			c=MyConnection.getConnection();
			ps=c.prepareStatement("SELECT * FROM "+DaoConstants.NOME_TABELLA_VERSO+" WHERE "+DaoConstants.COLONNA_ID_VERSO+"=?");
			ps.setInt(1, idva);
			rs=ps.executeQuery();
			if(rs.next())
			{
				verso=new Verso();
				verso=makeObject(verso,rs);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				ps.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return verso;
	}
	public List<Verso> findByExample(Verso verso)
	{
		Connection c=null;
		Statement stm=null;
		ResultSet rs=null;
		Verso verso1=null;
		List<Verso>list=new ArrayList<Verso>();
		String query="SELECT * FROM "+DaoConstants.NOME_TABELLA_VERSO+" WHERE 1=1 ";
		try
		{
			c=MyConnection.getConnection();
			if(verso.getCodice()!=null && !(verso.getCodice().equals("")))
			{
				query=query+" AND "+DaoConstants.COLONNA_CODICE_VERSO+" LIKE '"+verso.getCodice()+"%'";
			}
			if((verso.getDescrizione()!=null)&&!(verso.getDescrizione().equals("")))
			{
				query=query+" AND "+DaoConstants.COLONNA_DESCRIZIONE_VERSO+" LIKE '"+verso.getDescrizione()+"%'";
			}
			stm=c.createStatement();
			rs=stm.executeQuery(query);
			while(rs.next())
			{
				verso1=new Verso();
				verso1=makeObject(verso1,rs);
				list.add(verso1);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				stm.close();
				c.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return list;
	}
	public Verso makeObject(Verso verso,ResultSet rs) throws SQLException
	{
		verso.setIdVerso(rs.getInt(DaoConstants.COLONNA_ID_VERSO));
		verso.setCodice(rs.getString(DaoConstants.COLONNA_CODICE_VERSO));
		verso.setDescrizione(rs.getString(DaoConstants.COLONNA_DESCRIZIONE_VERSO));
		return verso;
	}
}
	

